﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace DuibaRefreshStateWindowsService
{
    public partial class DuibaRefreshStateService : ServiceBase
    {
        private readonly IScheduler scheduler;
        //时间间隔
        private readonly string StrCron = ConfigurationManager.AppSettings["cron"] == null ? "* 10 * * * ?" : ConfigurationManager.AppSettings["cron"];
        public DuibaRefreshStateService()
        {
            InitializeComponent();
            ISchedulerFactory factory=new StdSchedulerFactory();
            scheduler = factory.GetScheduler();
        }

        protected override void OnStart(string[] args)
        {
            if(!scheduler.IsStarted)
            {
                scheduler.Start();
                IJobDetail job = JobBuilder.Create<DuibaJob>().Build();
                ITrigger trigger = TriggerBuilder.Create().StartNow().WithCronSchedule(StrCron).Build();
                scheduler.ScheduleJob(job, trigger);
            }
        }

        protected override void OnStop()
        {
            if(!scheduler.IsShutdown)
            {
                scheduler.Shutdown();
            }
        }
        protected override void OnPause()
        {
            scheduler.PauseAll();
            base.OnPause();
        }
        protected override void OnContinue()
        {
            scheduler.ResumeAll();
            base.OnContinue();
        }
    }
}
